<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/18
 * Time: 10:03
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;

class Wechat extends Base
{
    /**
     * 根据openid查询记录
     * @param string $openid
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWechatByOpenid(string $openid = '')
    {
        return $this->where('openid', $openid)
            ->find();
    }

    /**
     * @param string $openid
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOpenidById(string $openid = '')
    {
        return $this->where('openid', $openid)
            ->find();
    }

    /**
     * @param array $where
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexWechatByCount(array $where = [])
    {
        return $this->field('id,count(*) as count')
            ->where($where)
            ->find();
    }

    /**
     * @param string $nickName
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWechatByNickName(string $nickName = '', string $field = '*'): array
    {
        return $this->field($field)
            ->where('nickName', 'like', "%{$nickName}%")
            ->select()
            ->toArray();
    }

    /**
     * 根据name模糊查询所有id
     * @param string $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWechatByRealNameByIds(string $name = ''): array
    {
        return $this->field('id,openid')
            ->where('relname', 'like', "%{$name}%")
            ->select()
            ->toArray();
    }
}